# Create your tests here.
from bs4 import BeautifulSoup
import requests
import re
temp_count = 0
for count in range(1, 100):
    req = requests.get(r'http://www.mzitu.com/page/%d/' % count)
    bs = BeautifulSoup(req.text,"html.parser")
    rule = re.compile(r'http://www.mzitu.com/\d+')
    for url in rule.findall(req.text)[:17]:
        req1 = requests.get(url)
        for j in range(20):
            url3 = url+'/%d/'
            req2 = requests.get(url3 % j)
            bs = BeautifulSoup(req2.text,'html.parser')
            try:
                xz = bs.find_all('img')[0]['src']
            except:
                pass
            print(xz)
            try:
                pic = requests.get(xz, timeout=3)
            except requests.exceptions.ConnectionError:
                print('【错误】当前图片无法下载')
                continue
            string = 'D:\\mzitu\\' + str(temp_count) + '.jpg'
            fp = open(string, 'wb')
            fp.write(pic.content)
            fp.close()
            temp_count += 1